from web.models import PricePolicy
from django.core.exceptions import ValidationError
from django import forms
from utils.bootstrap import BootStrapForm


class PolicyAddForm(BootStrapForm,forms.ModelForm):
    class Meta:
        model = PricePolicy
        fields = '__all__'

    def clean_price(self):
        price = self.cleaned_data['price']
        if price <= 0:
            raise ValidationError('价格必须是正数')
        return price

    def clean_count(self):
        count = self.cleaned_data['count']
        if count is not None:
            if count < 0:
                raise ValidationError("数量必须是正数")
            if count%10!=0:
                raise ValidationError("数量必须是100的倍数")
        return count